package vn.allmobile.ambnews.fragment;

import java.util.ArrayList;
import java.util.List;

import vn.allmobile.ambnews.R;
import vn.allmobile.ambnews.activities.main.MainAllPapersActivity;
import vn.allmobile.ambnews.data.CacheFile;
import vn.allmobile.ambnews.data.ListPaperData;
import vn.allmobile.ambnews.data.Paper;
import vn.allmobile.ambnews.network.NetworkAPI;
import vn.allmobile.ambnews.utils.Contants;
import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.aphidmobile.utils.IO;
import com.mobeta.android.dslv.DragSortListView;
import com.mobeta.android.dslv.DragSortListView.DropListener;

@SuppressLint("NewApi")
public class allpapers_sort_fragment extends Fragment {
	SortListViewAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View mView = (LinearLayout) inflater.inflate(
				R.layout.activity_allpapers_sort, container, false);
		DragSortListView lv = (DragSortListView) mView
				.findViewById(R.id.allpapers_sort_listview);
		lv.setDropListener(new DropListener() {
			
			
				
			@Override
			public void drop(int from, int to) {
				Log.e("fac", "Drop item");
				Paper paper = (Paper) mAdapter.getItem(from);

				mAdapter.notifyDataSetChanged();
				mAdapter.remove(paper);
				mAdapter.insert(paper, to);
				// Save to file
				CacheFile.saveFile(getActivity(),
						Contants.CACHE_ALLPAPERS_LIST, Contants.CACHE_PATH, IO
								.convertObjectToString(new ListPaperData(
										mAdapter.getList())));
				List<String> listId = new ArrayList<String>();
				// resort list Paper
				for(Paper pp : mAdapter.getList() )
				{
					listId.add(pp.getId());
				}
				
				
				List<Paper> sortedPaper = new ArrayList<Paper>();
				List<Paper> tmpsortedPaper = new ArrayList<Paper>();
				List<Paper> unsortedPaper = new ArrayList<Paper>();
				for(Paper pp : NetworkAPI._papers)
				{
					String id = pp.getId();
					if(listId.contains(id)){
						tmpsortedPaper.add(pp);
					}
					else
					{
						unsortedPaper.add(pp);
					}
				}
				
				for(String id : listId)
				{
					for(Paper pp :tmpsortedPaper)
					{
						if(id.equals(pp.getId()))
						{
							sortedPaper.add(pp);
						}
					}
				}
				
				NetworkAPI._papers.clear();
				NetworkAPI._papers.addAll(sortedPaper);
				NetworkAPI._papers.addAll(unsortedPaper);
				CacheFile.saveFile(getActivity(),
						Contants.CACHE_PAPER_NAME, Contants.CACHE_PATH, IO
								.convertObjectToString(new ListPaperData(
										NetworkAPI._papers)));
				//
				Activity mAc = getActivity();
				if (mAc instanceof MainAllPapersActivity) {
					((MainAllPapersActivity) mAc).datachanged();
				}
				
			}
		});
		//
		// Load file and load data
		List<Paper> mList = new ArrayList<Paper>();
		String mCachedFile = CacheFile.readFile(getActivity(),
				Contants.CACHE_ALLPAPERS_LIST, Contants.CACHE_PATH);
		if (mCachedFile != null) {
			ListPaperData mL = (ListPaperData) IO.convertStringToObject(
					mCachedFile, ListPaperData.class);
			mList = mL.getListPaper();
		}
		
		
		
		mAdapter = new SortListViewAdapter(this.getActivity()
				.getApplicationContext(), mList);
		lv.setAdapter(mAdapter);		
	
		
		
		return mView;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		Log.e("fac", "Off view");
		super.onDestroy();
	}

}
